package com.example.healthsystem.service.impl;

import com.example.healthsystem.exception.NotFoundException;
import com.example.healthsystem.exception.PersistenceException;
import com.example.healthsystem.service.UserService;
import com.example.healthsystem.mapper.UserMapper;
import com.example.healthsystem.entity.*;
import com.example.healthsystem.util.HashUtils;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
//public class UserServiceImpl implements UserService, UserDetailsService {
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;

    // --- this is override userDetailService
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
//        user user =userMapper.findByUsername(username);
//        if(user==null){
//            throw new UsernameNotFoundException("用户不存在");
//        }
//        return user;
//    }

    // --- this are override userService interface
    @Override
    public void registerUser(user user){
        if(userMapper.registerUser(user)!=1){
            throw new PersistenceException("无法注册该用户");
        }
    }

    @Override
    public List<user> findAlluser(int pageNum,int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.findAlluser();
    }

    @Override
    public String haveId(String id){
        return userMapper.haveId(id);
    }

    @Override
    public user findUserByUsernameAndPassword(String username,String password){
        user user=userMapper.findByUsername(username);
//        if(user==null){
//            throw new UsernameNotFoundException("用户不存在");
//        }
        // --- confirm user's password

        return user;
    }

    @Override
    public user finduserByusername(String username){
        user user=userMapper.findByUsername(username);
        return user;
    }

    @Override
    public void adminchangeProfile(user user){
        userMapper.adminchangeProfile(user);
    }
    @Override
    public void userchangeProfile(user user){
        userMapper.userchangeProfile(user);
    }



    @Override
    public void deleteUser(String username){
        if(userMapper.deleteUser(username)==0){
            throw new PersistenceException("无法删除该用户");
        }
    }
}
